package com.atguigu.gmall.realtime.joindemo;

import com.atguigu.gmall.realtime.app.BaseSQLApp;
import com.atguigu.gmall.realtime.util.SQLUtil;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/12/30 10:15
 */
public class RegularJoin_Left extends BaseSQLApp {
    public static void main(String[] args) {
        new RegularJoin_Left().init(
            5001,
            2,
            "RegularJoin_1"
        );
        
    }
    
    @Override
    public void handle(StreamExecutionEnvironment env,
                       StreamTableEnvironment tEnv) {
        // 当是左连接的时候: 左表的 ttl 是指左表超过 30s 没有 join, 才会把数据清除
        // 当是左连接的时候: 右表仍然是 30s 一定会删除
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(30));
        
        tEnv.executeSql("create table t1(" +
                            " id int, " +
                            " name string " +
                            ")" + SQLUtil.getKafkaSourceDDL("t1", "atguigu", "csv"));
    
        tEnv.executeSql("create table t2(" +
                            " id int, " +
                            " age int " +
                            ")" + SQLUtil.getKafkaSourceDDL("t2", "atguigu", "csv"));
        
      
        tEnv.sqlQuery("select " +
                          " t1.id," +
                          " name, " +
                          " age " +
                          "from t1 " +
                          "left join t2 on t1.id=t2.id")
            .execute()
            .print();
        
        
    
    }
}
/*
 左连接:
    特点 左边都要, 右表没有的用 null 来补齐

总结:
 内连接:
    join 的结果, 只有新增
 左连接:
    join 的结果, 有新增, 也有删除 (撤回)
 */